<!-- 设备动作的联动设备 -->

<template>
  <div>
    <EasyRow v-if="one.actionDevice === 1" col2>
      <FormSlot label="联动设备" bind="actionDeviceId" required>
        <span v-if="one.actionDeviceId" class="el-button--text" @click="toOpen">
          <span>{{ one.actionDeviceName }}</span>
        </span>

        <span v-if="!isShow">
          <BtnText v-if="one.actionDeviceId" title="清除" @click="toClear" />
          <BtnPrimary v-else title="选择" @click="toOpen" />
        </span>
      </FormSlot>

      <FormSelect
        v-if="function_readySelect"
        label="设备功能"
        bind="actionDeviceFunction"
        :dict="function_selectDict"
        :disabled="!one.actionDeviceId"
        required
      />
    </EasyRow>

    <EasyModal v-model="easyVisible" title="选择设备" medium @submit="toSubmit" @close="toReset">
      <ChooseIOTOne
        :obj="obj"
        bind="device"
        :deviceCategoryId="one.actionDeviceCategory"
        :deviceTypeId="one.actionDeviceType"
      />
    </EasyModal>
  </div>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'ActionDevice',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [
    CORE.mixWeb(),
    CORE.mixSelect('function', 'function_'),
  ],

  /**
   * 状态数据 (函数返回对象)
   */
  data() {
    return {
      easyVisible: false, // 弹窗是否可见
      obj: { device: {} }, // 数据对象
    }
  },

  /**
   * 监听属性 (深度 deep 首次 immediate)
   */
  watch: {
    /**
     * 监听弹窗可见
     */
    easyVisible(visible) {
      visible && this.toReset()
    },

    /**
     * 监听设备动作
     */
    'one.actionDevice': {
      immediate: true,
      handler(actionDevice) {
        actionDevice === 2 && this.toClear()
      },
    },

    /**
     * 监听联动设备的设备类型
     */
    'one.actionDeviceType': {
      immediate: true,
      handler(deviceType) {
        this.function_selectParam.deviceTypeCode = deviceType
        this.function_startSelect()
      },
    },
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 弹窗
     */
    toOpen() {
      this.easyVisible = true
    },

    /**
     * 提交
     */
    toSubmit() {
      const { obj, one } = this
      this.easyVisible = false

      const { deviceCategoryId, deviceTypeId, areaId, id, deviceName } = obj.device
      CORE.merge(one, {
        actionDeviceCategory: deviceCategoryId,
        actionDeviceType: deviceTypeId,
        actionDeviceArea: areaId,
        actionDeviceId: id,
        actionDeviceName: deviceName,
      })
    },

    /**
     * 重置
     */
    toReset() {
      const { one } = this
      const device = {
        id: one.actionDeviceId,
        deviceName: one.actionDeviceName,
        deviceCategoryId: one.actionDeviceCategory,
        deviceTypeId: one.actionDeviceType,
        areaId: one.actionDeviceArea,
      }
      this.obj = { device }
    },

    /**
     * 清除
     */
    toClear() {
      CORE.merge(this.one, {
        actionDeviceCategory: undefined,
        actionDeviceType: undefined,
        actionDeviceArea: undefined,
        actionDeviceId: undefined,
        actionDeviceName: undefined,
      })
      this.obj = { device: {} }
    },
  },
}
</script>
